<template>
  <div class="goodsCates">
    <!-- 搜索框 -->
    <div class="header-search">
      <input 
        type="text"
        placeholder="点击搜索商品信息"
        v-model="cates_kw"
        @keyup.enter="goToSearch"
      />
    </div>
    <!-- 官方商城 -->
    <div class="cates-wrap">
      <!-- 左侧导航-->
      <div class="cates-left">
        <div
          class="cates-left-title"
          v-for="(item, index) in category"
          :key="item.id"
        >
          <h3 :class="{ active: activeMenu == index }" @click="change(index)">
            {{ item.cate_name }}
          </h3>
        </div>
      </div>
      <!-- 右侧内容 -->
      <div class="rates-right-box">
        <div class="cates-right" @scroll="scrollEvent">
          <div
            class="con-title"
            v-for="item in category"
            :key="item.id"
            ref="item"
          >
            <h3>{{ item.cate_name }}</h3>
            <div class="cates-display">
              <div
                class="goodsList"
                v-for="list in item.children"
                :key="list.id"
              >
                <img :src="list.pic" alt="" />
                <p>{{ list.cate_name }}</p>
              </div>
            </div>
          </div>
          <div style="height: 340px"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ClassifyVue",

  data() {
    return {
      cates_kw: "",
      category: [],
      activeMenu: 0, // 左侧菜单高亮
      tops: [], // 存右侧区域滚动的高度
      initCalcTops: true,
      isSmoothing: true, // 是否在点击左侧菜单
      timer: null,
    };
  },

  updated() {
    if (this.initCalcTops) {
      let sumHeight = 0;
      this.tops.push(sumHeight);
      this.$refs.item.forEach((div) => {
        sumHeight += div.clientHeight;
        this.tops.push(sumHeight);
      });
      this.initCalcTops = false;
    }
  },
  methods: {
    goToSearch() {
      // console.log('---');
      if (this.cates_kw) {
        this.$router.push(`/ClassifyDetails/?aa=${this.cates_kw}`);
      } else {
        alert("请输入关键字");
      }
    },

    change(index) {
      this.isSmoothing = false;
      this.activeMenu = index;
      // console.log(this.$refs.item[index]);

      // 1. 锚点定位：利用a标签的href设置为某个元素的id值。(没法设置平滑的过渡效果，瞬间回到顶部区域)
      // 2. scrollTo({top: 100, behavie: 'smooth'}
      // this.$refs.scrollContainer.scrollTo({
      //   top: this.tops[index],
      //   behavior: "smooth", // 平滑过渡效果
      // });

      // 3. scrollIntoView(); 让指定的元素出现在视图的顶部
      // console.log(this.$refs.item[index]);
      this.$refs.item[index].scrollIntoView({
        behavior: "smooth", // 平滑过渡效果
      });
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.isSmoothing = true;
      }, 600);
      // 注意：使用scrollTo和scrollIntoView的平滑过渡效果，会影响元素的滚动事件。
    },

    scrollEvent(e) {
      if (this.isSmoothing) {
        const scrollTop = e.target.scrollTop;
        // console.log(scrollTop);
        // 随着滚动不断地获取滚动高度，当前的滚动高度是否小于遍历的高度，如果小于就把当前的索引返回来
        const idx = this.tops.findIndex(
          (totalHeight) => scrollTop < totalHeight
        );
        // console.log(this.tops);
        //  [ 0, 697, 970, 1349, 1516, 1789, 2062, 2229, 2502, 2775, … ]
        // console.log(idx);
        this.activeMenu = idx - 1;
      }
    },

    beforeDestroy() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
  },

  created() {
    this.$request("http://demo26.crmeb.net/api/category").then((data) => {
      this.category = data.data;
      console.log(this.category);
    });
  },
};
</script>

<style lang="scss" scoped>
.goodsCates {
  .header-search {
    height: 50px;
    width: 100%;
    background-color: #fff;
    border-bottom: 1px solid #eee;
    input {
      display: block;
      background-color: #eee;
      margin: 9px auto;
      width: 90%;
      height: 35px;
      border: none;
      font-size: 12px;
    }
  }
  .cates-wrap {
    display: flex;
    .cates-left {
      height: calc(100vh - 54px - 50px);
      .cates-left-title {
        width: 70px;
        h3 {
          font-weight: normal;
          font-size: 13px;
          height: 55px;
          line-height: 55px;
          text-align: center;
          &.active {
            color: #ff448f;
            background: #fff;
          }
          &.active::before {
            content: "";
            position: absolute;
            top: 50;
            left: 0;
            width: 2px;
            height: 55px;
            background: #ff448f;
          }
        }
      }
    }
    .rates-right-box {
      .cates-right {
        width: calc(100vw - 70px);
        height: calc(100vh - 54px - 50px);
        overflow-y: scroll;
        background-color: #fff;
        &::-webkit-scrollbar {
          display: none;
        }
        .con-title {
          h3 {
            font-size: 15px;
            color: #333;
            margin: 0 16px;
            font-weight: 700;
            text-align: center;
            height: 55px;
            line-height: 55px;
            position: relative;
            &:before {
              content: "";
              width: 50px;
              height: 1px;
              background-color: #eee;
              position: absolute;
              bottom: 28px;
              left: 40px;
            }
            &:after {
              content: "";
              width: 50px;
              height: 1px;
              background-color: #eee;
              position: absolute;
              bottom: 28px;
              right: 40px;
            }
          }
          .cates-display {
            display: flex;
            flex-wrap: wrap;
            .goodsList {
              width: 33%;
              display: flex;
              flex-direction: column;
              align-items: center;
              img {
                width: 66px;
                height: 66px;
              }
              p {
                font-size: 13px;
                color: #333;
                height: 30px;
                line-height: 30px;
                width: 66px;
                text-align: center;
              }
            }
          }
        }
      }
    }
  }
}
</style>
